<?php
function paging($sql,$page,$limit,$class='',$param='',$self='',$loop='',$start='',$paggingNumber='')
{
	if (!strlen($self)) $self = $_SERVER['PHP_SELF'];
	if (strlen($param)) $param = "&".$param;
	if (strstr(strtolower($sql),"limit"))
	{
		$sql = substr($sql,0,strpos(strtolower($sql),"limit"));
	}
	
	//echo "<br><br>".$sql.'<br><br>';
	
	$res = mysql_query($sql);
	$numrows = mysql_num_rows($res);
	 $pages = ceil($numrows/$limit);
	
	 //echo 'page==='.$page.'<br>';
	 $tt=$page;//$_REQUEST['page'];
	if($paggingNumber == 2)
	{
		if(!isset($_REQUEST['loop']))
		{
			$loprequest=$loop;//display page end limit
		}
		else
		{
			//$loprequest=$numrows;
			$loprequest=$loop;
		}
		if(!isset($_REQUEST['start']))
		{
			$_startlop=$start;//display page end limit
		}
		else
		{
			//$loprequest=$numrows;
			$_startlop=$start;
		}
			
	}
	else
	{
		$loprequest=$_REQUEST['loop'];//$loop;//display page end limit
		$_startlop=$_REQUEST['start'];//$start;//display page end limit
	}
	

	
	//$loprequest=$_REQUEST['loop'];
	//echo 'fdgdfgdfg===='.$loprequest.'<br><br>';
	
	//$_startlop=$_REQUEST['start'];//display page start limit
	//echo 'start===='.$_startlop.'<br><br>';
	if($_startlop=='')
	$_startlop=0;

$gg=$tt/$limit;//calsulate no page
//echo $gg.'=======mm===';
if($gg==0)
$gg=1;
if($gg<$loprequest)
{
$gg=$_startlop;
$lop=$loprequest;
}else
{
$_startlop=$gg;
$lop=$gg+10;//display 10 page
}

if($lop>$pages)
$lop=$pages;
	if ($page != 0)
	{
		$back_page = $page - $limit; 
		$str .="<a href=$self?page=0"."$param class=$class><<&nbsp;&nbsp;</a>\n";
		if(isset($str))
		$str .="<a href=$self?page=$back_page&loop=$lop&start=$_startlop"."$param class=$class><</a>\n";
		else
		$str ="<a href=$self?page=$back_page&loop=$lop&start=$_startlop"."$param class=$class><</a>\n";
		if($_startlop>9 && $pages>2)
		{
		if(isset($str))
		{
				
				$str .="<a href=$self?page=0&loop=0&start=0"."$param class=$class>1</a> \n";
				$str .="<a href=$self?page=$limit&loop=0&start=0"."$param class=$class>2</a> \n...";
				}
				
		}
	} 


	if ($pages > 1)
	{
		for ($i=$gg;$i<=$lop;$i++)
		{ 
			
			$ppage = $limit * ($i - 1);
			
			if ($ppage == $page)
			{ 
				
				if(isset($str))
				{
				if($i==$gg)
				{
				$str .="<b><font color='#CCCCCC'>$i</font></b>\n";
				}
				else{
				$str .="<b>-<font color='#CCCCCC'>$i</font></b>\n";
				}
				}
				else
				{
				if($i==$gg)
				{
				$str ="<b><font color='#CCCCCC'>$i</font></b>\n";
				}else{
				$str ="<b>-<font color='#CCCCCC'>$i</font></b>\n";
				}
				}
			}
			else
			{
				if($i==$gg)
				{
				$str .="<a href=$self?page=$ppage&loop=$lop&start=$_startlop"."$param class=$class>$i</a> \n";
				}else{
				$str .="<a href=$self?page=$ppage&loop=$lop&start=$_startlop"."$param class=$class>-$i</a> \n";
				}
			}
			
		}
	}
	if($pages>10 && $lop<$pages)
	{
	$lastppage1= $limit * ($pages - 2);
	$lastppage = $limit * ($pages - 1);
	$prevlastdisplay=$pages-1;
	$lastdisplay=$pages;
	$startpage1=$pages-8;
	$lastpage2=$pages-9;
	$str .="..<a href=$self?page=$lastppage1&loop=$pages&start=$startpage1"."$param class=$class>$prevlastdisplay</a></b>\n";
	$str .="<a href=$self?page=$lastppage&loop=$pages&start=$lastpage2"."$param class=$class>$lastdisplay</a></b>\n";
	}
	
	if (!((($page+$limit) / $limit) >= $pages) && $pages != 1)
	{
		$next_page = $page + $limit;
		$lastpage=($pages*$limit)-$limit;
		
		$str .="<a href=$self?page=$next_page&loop=$lop&start=$_startlop"."$param class=$class>></a>"; 
		$str .="<a href=$self?page=$lastpage&loop=$lop&start=$_startlop"."$param class=$class>&nbsp;&nbsp;>></a>"; 
	}
	if(isset($str)) return $str;
}
?>